<?php
/**
 * 系统初始化类
 * 
 * @file App.class.php
 * @create 2012-08-02
 * @version v1.0
 * @author veapon 
 * @copyright (C) veapon.com
 */
if( !defined('__ROOT__') ) die('Access Denied');
class App{

	/**
	 * 系统执行
	 * 
	 * @return 
	 * @since v1.0
	 * @create 2012-08-03
	 */
	public static function run(){
		self::init();
		Controller::load(G('MODULE').'.'.G('CONTROLLER'))->run(G('ACTION'));	
	}

	/**
	 * 系统初始化
	 * 
	 * @return 
	 * @since v1.0
	 * @create 2012-08-03
	 */
	private static function init(){
		//加载核心
		$corePath = __LIB__.'core/';
		include_once($corePath.'Core.class.php');
		if( !is_dir($corePath) ) die("Core path $corePath not found");
		$h = opendir($corePath);
		while( ($file=readdir($h)) !== false ){
			if( preg_match("/^\w+\.class\.php$/",$file) ){
				include_once($corePath.$file);
			}
		}	
		//加载模型
		include_once(__LIB__.'db/DB.class.php');
		include_once(__LIB__.'db/Model.class.php');
		closedir($h);
		$module = C('default_module') ? C('default_module') : 'index';
		$controller = C('default_contoller') ? C('default_contoller') : 'index';
		$action = C('default_action') ? C('default_action') : 'index';
		define('DEFAULT_MODULE', $module);
		define('DEFAULT_CONTROLLER', $controller);
		define('DEFAULT_ACTION', $action);
		define('__VAR__', __ROOT__.'var/');
		define('VAR_URL',__URL__.'var/');
		define('__APP__', __ROOT__.'app/');
		define('__MODEL__', __APP__.'models/');
		define('__CONTROLLER__', __APP__.'controllers/');
		define('__TABLE__', __APP__.'tables/');
		define('__VIEW__', __APP__.'views/');
		//URL解释
		Router::init();
	}

	/**
	 * 生成核心缓存函数，待完成
	 * 
	 * @return 
	 * @since v1.0
	 * @create 2012-08-06
	 * @todo: 获取PHP文件内容，并替换注释
	 */
	public static function build_runtime(){
		$corePath = __LIB__.'core/';
		if( !is_dir($corePath) ) return false;
		$h = opendir($corePath);
		$ct = '<?php ';
		$reg = "/\/\*(\s|.)*\*\//";
		while( ($file=readdir($h)) !== false ){
			if( preg_match("/^\w+\.class\.php$/",$file) ){
				$tmp = file_get_contents($corePath.$file);
				//echo $tmp;
				//$tmp = substr($tmp,5);
				//$ct .= preg_replace($reg, "", $tmp); 
			}
		}
		closedir($h);
		//return $ct;
	}
}
